/**
 * 安全日志ES查询体
 */

let queryBody = {
  from: 0,
  size: 10,
  query: {
    bool: {
      must: [
        {
          query_string: {
            query: '*'
          }
        },
        {
          query_string: {
            query: '*'
          }
        }
      ],
      filter: []
    }
  },
  sort: {
    ts: {
      order: 'desc'
    }
  }
}

let sendData = {
  es_data: '',
  data_source: '["AIR"]',
  flag: 0,
  startTime: '',
  endTime: ''
}

// 根据路由设置类型
export const setCondition = (router) => {
  let query = router === 'all' ? '*' : `type: ${router}`
  queryBody.query.bool.must[1].query_string.query = query
  queryBody.query.bool.filter = []
  // if (router === 'all') {
  //   queryBody.query.bool.filter = []
  // } else {
  //   // 获取级别>=1的日志
  //   queryBody.query.bool.filter = [
  //     {
  //       range: {
  //         level: {
  //           gte: 1 // >=
  //         }
  //       }
  //     }
  //   ]
  // }
}

// 设置 搜索框条件
export const setEsQuery = info => {
  queryBody.query.bool.must[0].query_string.query = info === '' ? '*' : info
}

export const setSort = (prop, order) => {
  queryBody.sort = { [prop]: { order: order } }
}

export const setPage = (current, pageSize) => {
  queryBody.from = (current - 1) * pageSize
  queryBody.size = pageSize
}
// 过滤条件
export const setEsFilter = filter => {
  queryBody.query.bool.must = [queryBody.query.bool.must[0], queryBody.query.bool.must[1], ...filter]
}

export const setEsAggs = aggs => {
  queryBody.aggs = aggs
}

// 获取 当前状态的 的QueryBody
export const getEsQueryBody = () => {
  return JSON.stringify(queryBody)
}

// 获取 搜索列表 的QueryBody
export const getEsSearch = () => {
  queryBody.aggs = {}
  return JSON.stringify(queryBody)
}

// 获取 标签聚合 的QueryBody
export const getEsAgg = () => {
  queryBody.aggs = {}
  return JSON.stringify(queryBody)
}

// 待发送数据
export const getSendData = (queryBody, time) => {
  sendData.es_data = queryBody
  if (time.length !== 0) {
    sendData.startTime = `${time.split(' ')[0]} ${time.split(' ')[1]}`
    sendData.endTime = `${time.split(' ')[3]} ${time.split(' ')[4]}`
  } else {
    sendData.startTime = ''
    sendData.endTime = ''
  }
  return JSON.stringify(sendData)
}
